$(function () {
  var school_datagrid = $("#school_datagrid");
  var school_dialog = $("#school_dialog");
  var school_form = $("#school_form");
  var changeState_btn = $("#changeState_btn");
  school_datagrid.datagrid({
    url: '/school/query.do',
    fit: true,
    fitColumns: true,
    columns: [[
      {field: 'id', title: '编号', width: 100,},
      // {field: 'college',hidden:true},
      {field: 'school', title: '学校', width: 100,},
      {field: 'site', title: '地址', width: 100,},
      {field: 'importance', title: '重要度', width: 100,},
      {field: 'intentionDegree', title: '意向度', width: 100,},
      {field: 'phone', title: '学校电话', width: 100,},
      {field: 'intentionSubject', title: '意向学科', width: 100,},
      {
        field: 'linkmanContacts', title: '联系人', width: 100, formatter: function (value, row, index) {
        return value ? value.name : '';
      }
      },
      {field: 'marketingPersonnel', title: '营销人员', width: 100,},
      {field: 'followStaff', title: '跟进人员', width: 100,},
      {field: 'lastFollowTime', title: '上次跟进时间', width: 100,},
      {field: 'nextFollowTime', title: '下次跟进时间', width: 100,},
      {
        field: 'followState', title: '跟进状态', width: 100, formatter: function (value) {
        return value ? '已跟进' : '<font color="red">待跟进</font>'
      }
      },
      {
        field: 'serviceState', title: '客服状态', width: 100, formatter: function (value) {
        return value ? '已签约' : '<font color="red">未签约</font>'
      }
      },
    ]],
    singleSelect: true,
    pagination: true,
    toolbar: "#school_btns",
    sortName: 'hireDate',
    sortOrder: 'desc',
    onClickRow: function (index, row) {
      if (row.serviceState) {
        changeState_btn.linkbutton({
          text: '取消签约'
        })
      } else {
        changeState_btn.linkbutton({
          text: '签约'
        })
      }
    }
  });
  school_dialog.dialog({
    width: 900,
    height: 600,
    buttons: "#save_btns",
    closed: true,
    onClose: function () {
      school_form.form('clear');
    }
  });
  var methodObj = {
    add: function () {
      school_dialog.dialog('setTitle', '大客户');
      school_dialog.dialog('open');
    },

    edit: function () {
      var row = school_datagrid.datagrid("getSelected");
      if (!row) {
        $.messager.alert('温馨提示', "请选中一条数据", 'warning');
        return;
      }
     row['linkmanContacts.id'] = row.linkmanContacts.id;
      console.log(row);

      school_form.form('load', row);
      school_dialog.dialog('setTitle', '编辑大客户');
      school_dialog.dialog('open');
    },
    exportXls: function () {
      //导出
      window.location.href = "/school/exportXls.do";
    },

    changeState: function () {
      var row = school_datagrid.datagrid("getSelected");
      if (!row) {
        $.messager.alert('温馨提示', "请选中一条数据", 'warning');
        return;
      }
      $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
        if (r) {
          $.get("/school/changeState.do", {id: row.id}, function (data) {
            if (data.success) {
              $.messager.alert('温馨提示', '操作成功', 'info')
              school_datagrid.datagrid('reload');
            } else {
              $.messager.alert('温馨提示', data.msg, 'warning');
            }
          })
        }
      });
    },
    del: function () {
      var row = school_datagrid.datagrid("getSelected");
      if (!row) {
        $.messager.alert('温馨提示', "请选中一条数据", 'warning');
        return;
      }
      $.messager.confirm('确认对话框', '您确定要删除该打客服吗？', function (r) {
        if (r) {
          $.get("/school/delete.do", {id: row.id}, function (data) {
            if (data.success) {
              $.messager.alert('温馨提示', '删除成功', 'info')
              school_datagrid.datagrid('reload');
            } else {
              $.messager.alert('温馨提示', data.msg, 'warning');
            }
          })
        }
      });
    },
    reload: function () {
      school_datagrid.datagrid('reload');
    },

    save: function () {
      school_form.form('submit', {
        url: '/school/saveOrUpdate.do',
        success: function (data) {
          data = $.parseJSON(data);
          if (data.success) {
            $.messager.alert('温馨提示', '保存成功', 'info')
            methodObj.cancel();
            school_datagrid.datagrid('reload');
          } else {
            $.messager.alert('温馨提示', data.msg, 'error');
          }
        }
      })
    },

    cancel: function () {
      school_dialog.dialog('close');
    },
    changeState: function () {
      var row = school_datagrid.datagrid("getSelected");
      if (!row) {
        $.messager.alert('温馨提示', "请选中一条数据", 'warning');
        return;
      }
      var state = row.serviceState ? '取消签约' : '签约';
      //var flag =row.serviceState ? 0:1;

      $.messager.confirm('确认对话框', '您确定要跟该客服' + state + '么？', function (r) {
        /*var id = school_datagrid.datagrid("getSelected").id;*/
        if (r) {
          $.get("/school/changeState.do", {id: row.id}, function (data) {
            if (data.success) {
              $.messager.alert('温馨提示', '签约成功', 'info')
              school_datagrid.datagrid('reload');
            } else {
              $.messager.alert('温馨提示', data.msg, 'warning');
            }
          })
        }
      });

    },
    //查看
    see: function () {
      var school = school_datagrid.datagrid("getSelected");

      if (!school) {
        $.messager.alert('温馨提示', '请选中一条数据', 'info');
        return;
      }
      school_form.form("clear");
      school_dialog.dialog("setTitle", "数据");

      //联系人
      if (school.contact) {
        school["contact.id"] = school.contact.id;
      }
      //重要性
      if (school.importance) {
        school["importance.id"] = school.importance.id;
      }
      //意向程度
      if (school.wantedLevel) {
        school["wantedLevel.id"] = school.wantedLevel.id;
      }
      //意向学科
      if (school.subject) {
        school["subject.id"] = school.subject.id;
      }
      //意向学区
      if (school.college) {
        school["college"] = school.college;
      }
      //营销人员
      if (school.marketer) {
        school["marketer.id"] = school.marketer.id;
      }
      //跟进人员
      if (school.tracer) {
        school["tracer.id"] = school.tracer.id;
      }
      //学校类型
      if (school.type) {
        school["type.id"] = school.type.id;
      }

      school_form.form("load", school);

      $("#school_form input").prop("disabled", true);

      school_dialog.dialog("open");
    },
    query: function () {
      var keyword = $("[name=keyword]").val();
      var subject = $("[name=subject]").val();
      school_datagrid.datagrid('load', {
        keyword: keyword,
        subject: subject
      })
    },

  }
  $("a[data-cmd]").click(function () {
    var methodName = $(this).data('cmd');
    methodObj[methodName]();
  })


})



